package com.corona.game.util;

import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.math.Vector2;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import org.jetbrains.annotations.NotNull;

/* loaded from: pokewilds.jar:com/pkmngen/game/util/Json.class */
public class Json {
    public static final Gson gson = createGson();

    @NotNull
    private static Gson createGson() {
        return new GsonBuilder().registerTypeAdapter(Vector2.class, new Vector2TypeAdapter()).registerTypeAdapter(GridPoint2.class, new GridPoint2TypeAdapter()).enableComplexMapKeySerialization().create();
    }

    /* access modifiers changed from: private */
    /* loaded from: pokewilds.jar:com/pkmngen/game/util/Json$KeyTypeAdapter.class */
    public static abstract class KeyTypeAdapter<T> implements JsonDeserializer<T>, JsonSerializer<T> {
        protected abstract int getKey(T t);

        protected abstract T getCoordinate(int i);

        private KeyTypeAdapter() {
        }

        @Override // com.google.gson.JsonDeserializer
        public final T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return getCoordinate(json.getAsJsonPrimitive().getAsInt());
        }

        @Override // com.google.gson.JsonSerializer
        public final JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(Integer.valueOf(getKey(src)));
        }
    }

    /* loaded from: pokewilds.jar:com/pkmngen/game/util/Json$Vector2TypeAdapter.class */
    public static class Vector2TypeAdapter extends KeyTypeAdapter<Vector2> {
        private Vector2TypeAdapter() {
            super();
        }

        @Override
        public int getKey(Vector2 coordinate) {
            return (((int) coordinate.x) << 16) | (((int) coordinate.y) & 65535);
        }

        @Override // com.corona.game.util.Json.KeyTypeAdapter
        public Vector2 getCoordinate(int key) {
            return new Vector2().set((float) (key >> 16), (float) ((short) key));
        }
    }

    /* loaded from: pokewilds.jar:com/pkmngen/game/util/Json$GridPoint2TypeAdapter.class */
    public static class GridPoint2TypeAdapter extends KeyTypeAdapter<GridPoint2> {
        private GridPoint2TypeAdapter() {
            super();
        }

        @Override
        public int getKey(GridPoint2 coordinate) {
            return (coordinate.x << 16) | (coordinate.y & 65535);
        }

        @Override // com.corona.game.util.Json.KeyTypeAdapter
        public GridPoint2 getCoordinate(int key) {
            return new GridPoint2().set(key >> 16, (short) key);
        }
    }
}
